package com.heny.demo.controller;

import com.heny.demo.entity.TurnPoint;
import com.heny.demo.service.ITurnPointService;
import com.heny.demo.strategy.FileImportStrategy;
import com.heny.demo.strategy.StrategyFactory;
import com.heny.demo.tools.CoordinateTransformUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@RestController
public class TurnPointController {

    @Autowired
    private ITurnPointService turnPointService;

    @Autowired
    private StrategyFactory strategyFactory;


    @PostMapping("/upload")
    public Boolean uploadTurnPoint(MultipartFile turnPointFile) {
        try {
            // 根据文件类型选择策略
            FileImportStrategy strategy = strategyFactory.getStrategy(turnPointFile.getOriginalFilename());
            List<TurnPoint> turnPoints = strategy.importData(turnPointFile.getResource().getFile());

            // 转换坐标并保存到数据库
            turnPoints.forEach(turnPoint -> {
                try {
                    // 假设这里调用坐标转换工具类
                    turnPoint.setCoordinate(CoordinateTransformUtil.transformPoint(turnPoint.getCoordinate(), "EPSG:4548", "EPSG:4326"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            // 批量插入数据库
            turnPointService.saveBatch(turnPoints);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}